package org.weasel.wizards;

import java.util.ArrayList;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.weasel.business.Developer;
import org.weasel.business.Item;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Settings;
import org.weasel.views.Navigator;

public class UserStoryWizardPage2 extends WizardPage {
	//private Text userstoryNumber;
    private Combo estimatedEffort;
    private Combo estimatedRisk;
    private Combo estimatedTime;
    private List developerList;
    private List selectedList;
    private ArrayList<String> selectedDevelopers;
	private ISelection selection;
	private Project project;
	private Item item;

	public UserStoryWizardPage2(ISelection selection, Item item, Project project) {
		super("wizardPage");
		this.project = project;
		this.item = item;
		setTitle("Userstory Wizard");
        setDescription("Define the settings of the new userstory.");
        this.selection = selection;
        selectedDevelopers = new ArrayList<String>();
        if(item instanceof Iteration) {
        	setPageComplete(false);
        } else {
        	setPageComplete(true);
        }
	}
	
	public void createControl(Composite parent) {
		{
			parent.setSize(525, 223);
		}
		// TODO Auto-generated method stub
		Composite container = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        container.setLayout(layout);
        
        layout = new GridLayout();
        layout.numColumns = 4;
        TabFolder folder = new TabFolder(container, SWT.NULL);
        GridData gd = new GridData(GridData.FILL_BOTH);
        folder.setLayout(layout);
        folder.setLayoutData(gd);
          
        TabItem projectTabItem = new TabItem(folder, SWT.NULL);
        projectTabItem.setText("Userstory details");
        
       
        //initialize first tabitem
        Composite group = new Composite(folder, SWT.NULL);
        projectTabItem.setControl(group);
        group.setLayout(layout);        
        
        gd = new GridData(GridData.FILL_VERTICAL);
        gd.horizontalSpan = 2;
        gd.verticalIndent = 9;
        
        Label label = new Label(group, SWT.NULL);
        label.setText("Estimated Effort:");
        estimatedEffort = new Combo(group, SWT.NONE);
        initialize(estimatedEffort, 3);
        estimatedEffort.setLayoutData(gd);
        label = new Label(group, SWT.NULL);
        label.setText("          ");
        
        
        label = new Label(group, SWT.NULL);
        label.setText("Estimated Risk:");
        estimatedRisk = new Combo(group, SWT.NONE);
        estimatedRisk.add("Low");
        estimatedRisk.add("Medium");
        estimatedRisk.add("High");
        estimatedRisk.select(0);
        estimatedRisk.setLayoutData(gd);
        label = new Label(group, SWT.NULL);
        label.setText("               ");
        
        label = new Label(group, SWT.NULL);
        label.setText("Estimated Time:");
        estimatedTime = new Combo(group, SWT.NONE);
        initialize(estimatedTime, 10);
        estimatedTime.setLayoutData(gd);
        
        //if the item is an iteration then show the developer list
		if (item instanceof Iteration) {
			TabItem developers = new TabItem(folder, SWT.NULL);
	        developers.setText("Developers");
			//initialize second tabitem  
			layout = new GridLayout();
			layout.numColumns = 4;
			group = new Composite(folder, SWT.NONE);
			developers.setControl(group);
			group.setLayout(layout);
			gd = new GridData(GridData.FILL_BOTH);
			Group developerGroup = new Group(group, SWT.NONE);
			developerGroup.setLayout(layout);
			developerGroup.setLayoutData(gd);
			developerGroup.setText("Select developers:");
			developerList = new List(developerGroup, SWT.V_SCROLL | SWT.MULTI);
			developerList.setLayoutData(gd);
			initialize(developerList);
			Button button = new Button(group, SWT.NONE);
			button.setImage(new Image(Display.getCurrent(), getClass()
					.getResourceAsStream("/icons/left_arrow_nav.gif")));
			button.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseDown(MouseEvent e) {
					removeSelectedDevelopers();
				}
			});
			button = new Button(group, SWT.NONE);
			button.setImage(new Image(Display.getCurrent(), getClass()
					.getResourceAsStream("/icons/right_arrow_nav.gif")));
			button.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseDown(MouseEvent e) {
					getSelectedDevelopers();
				}
			});
			developerGroup = new Group(group, SWT.NONE);
			developerGroup.setLayout(layout);
			developerGroup.setLayoutData(gd);
			developerGroup.setText("Selected developers:");
			selectedList = new List(developerGroup, SWT.V_SCROLL | SWT.MULTI);
			selectedList.setLayoutData(gd);
		}
		setControl(container);
    }

    protected void removeSelectedDevelopers() {
		// TODO Auto-generated method stub
    	for (int i = 0; i < selectedList.getSelectionCount(); i++) {
    		selectedDevelopers.remove(selectedList.getSelection()[i]);
    		developerList.add(selectedList.getSelection()[i]);
    	}
    	selectedList.remove(selectedList.getSelectionIndices());
    	setPageComplete(isComplete());
    	
	}

	protected void getSelectedDevelopers() {
		// TODO Auto-generated method stub
    	for (int i = 0; i < developerList.getSelectionCount(); i++) {
    		selectedDevelopers.add(developerList.getSelection()[i]);
    		selectedList.add(developerList.getSelection()[i]);
    	}
    	developerList.remove(developerList.getSelectionIndices());
    	setPageComplete(isComplete());
	}

	private boolean isComplete() {
		// TODO Auto-generated method stub
		if (selectedList.getItemCount() != 0) {
			return true;
		} else {
			setErrorMessage("Developers must be specified");
			return false;
		}
	}

	private void initialize(List list) {
		for (Developer developer : project.getDevelopers()) {
			list.add(developer.getName() + " (" + developer.getId() + ")");
		}
	}

	private void initialize(Combo comboBox, int until) {
		// TODO Auto-generated method stub
    	for (int i = 1; i <= until; i++) 
    		comboBox.add(Integer.toString(i));
    	comboBox.select(0);
	}
     
    public String getEstimatedEffort() {
    	return estimatedEffort.getText();
    }
    
    public String getEstimatedTime() {
    	return estimatedTime.getText();
    }
    
    public String getEstimatedRisk() {
    	return estimatedRisk.getText();
    }
    
    public ArrayList<Developer> getDevelopers() {
    	IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewPart viewPart = page.findView(Navigator.ID);
		Navigator navigator = (Navigator) viewPart;
		Settings sett = navigator.getSettings();
		ArrayList<Developer> selDevelopers = new ArrayList<Developer>();
    	for (String string : selectedDevelopers) {
    		//this line retrieves the ID of the selected developers
			String developerID = string.substring(string.indexOf("(") + 1, string.length() - 1);
			selDevelopers.add(sett.getDeveloperByID(developerID));
		}
    	return selDevelopers;
    }
}
